package com.example.demo.application.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.application.common.MyConstantsRespone;
import com.example.demo.application.common.base.ResultMsg;
import com.example.demo.application.model.LossMsgEntity;
import com.example.demo.application.model.LossUserEntity;
import com.example.demo.application.threadutil.BlockModel;
import com.example.demo.application.threadutil.MyThreadPoolExecutor;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "池接口")
@RestController
@EnableAutoConfiguration
//@CrossOrigin(origins = "http://localhost:9000") :表明该方法允许来自http://localhost:9000访问，也就是前端可以是localhost:9000。
@RequestMapping(path = "/pool")
public class PoolController {

	protected final Logger log = LoggerFactory.getLogger(PoolController.class);

	@ApiOperation(value = "获得爬虫线程", notes = "获得爬虫线程")
	@RequestMapping(path = "/getblock", method = RequestMethod.GET)
	@ResponseBody
	public ResultMsg getblock() {
		ResultMsg modelMap = new ResultMsg();
		MyThreadPoolExecutor th = MyThreadPoolExecutor.getInstance();
		List<BlockModel> list = new ArrayList<BlockModel>();
		for (int i = 1; i < 999; i++) {
			BlockModel m = new BlockModel();
			int size = th.getBlockSize(i);
			if (size == -1) {
				break;
			}
			m.setName(MyThreadPoolExecutor.pname + i);
			m.setSize(size);
			list.add(m);
		}
		modelMap.setData(list);
		modelMap.setCode(MyConstantsRespone.success);
		return modelMap;
	}

	@ApiOperation(value = "获得爬虫线程", notes = "获得爬虫线程")
	@RequestMapping(path = "/getEventList", method = RequestMethod.GET)
	@ResponseBody
	public ResultMsg getEventList(String page, String keywords) {
		log.info(page+"-----"+keywords);
		ResultMsg rmg = new ResultMsg();
		LossUserEntity lu = new LossUserEntity();
		lu.setId("4771");
		lu.setName("文强");
		lu.setHead_image("http://dummyimage.com/80x80/FF6600");
		LossMsgEntity le = new LossMsgEntity();
		le.setContact("18234544889");
		le.setUser(lu);
		le.setStatus("已完成");
		le.setPrice("9.9");
		le.setType("丢失");
		le.setAddress("东区饭堂");
		le.setContent("理样又上飞光任节国府眼口打用院果。次海包深算群山斗组学制身马。文被可列比造受空取列等教全张。九儿记带义以极风与全程些之些权华革。况温山白心派级品压战们以非。");
		le.setThingsType("雨伞");
		le.setImages(new String[]{"http://dummyimage.com/350x350/FF6600","http://dummyimage.com/350x350/FF6600"});
		
		List<LossMsgEntity> list = new ArrayList<LossMsgEntity>();
		list.add(le);
		list.add(le);
		rmg.setData(list);
		rmg.setCode(MyConstantsRespone.success);
		if (Integer.parseInt(page) > 5) {
			rmg.setHas_more(false);
		}
		else {
			rmg.setHas_more(true);
		}
		return rmg;
	}
}
